// 对应首页的TabView
//  StatusListViewModel.swift
//  WeiboSwift
//
//  Created by Apple13 on 16/7/14.
//  Copyright © 2016年 yupeng. All rights reserved.
//

import UIKit

class StatusListViewModel: NSObject {

    //微博数据源
    lazy var statusList:[StatusViewModel] = [StatusViewModel]()
    
    //加载微博数据
    func loadStatus(callBack: (isSuccess: Bool)->()) -> Void {
       
        NetWorkTools.shareTools.loadStatus { (response, error) in
            if error != nil{
                print(error)
                callBack(isSuccess: false)
                return
            }
            //此处回调成功
            
            guard let dict = response as? [String:AnyObject] else {
                print("不是一个正经的字典")
                callBack(isSuccess: false)
                return
            }
            
            //此处字典格式正确,接下来判断内层字典格式是否正确
            guard let statusDicArray = dict["statuses"] as? [[String:AnyObject]] else {
                print("又不是不是一个正经的字典")
                callBack(isSuccess: false)
                return
            }
            
            //遍历字典内的数组and转模型
            var tempArray = [StatusViewModel]()
            for stastusDic in statusDicArray{
                let status = WBStatus(dict:stastusDic )
                
                //初始化Viewmodel 通过我们的微博模型
                let statusViewModel = StatusViewModel(status:status)
                
                tempArray.append(statusViewModel)
                
            }
            self.statusList.appendContentsOf(tempArray)
            
            callBack(isSuccess: true)
        }
        
        
        
    }
    
}
